package com.newbingo.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import javax.transaction.Transaction;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.hibernate.transform.RootEntityResultTransformer;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.newbingo.dao.imple.IGameDao;
import com.newbingo.entity.Game;
import com.newbingo.entity.GamePlay;
import com.newbingo.entity.GameTable;
import com.newbingo.util.Pager;

public class GameDao extends HibernateDaoSupport implements IGameDao {
	private static final Log log = LogFactory.getLog(GameDao.class);

	/**
	 * 
	 * 添加新游戏方法
	 */
	public void save(Game transientInstance) {
		log.debug("saving Game instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/**
	 * 删除游戏，级联删除游戏玩法,游戏桌游戏投注格
	 */
	public void delete(Game persistentInstance) {
		log.debug("deleting Game instance");
		try {
			Game game=this.findById(persistentInstance.getGameno());
			getHibernateTemplate().delete(game);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/*
	 * 
	 */
	public Game findById(java.lang.Integer id) {
		log.debug("getting Game instance with id: " + id);
		try {
			Game instance = (Game) getSession().get("com.newbingo.entity.Game",
					id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/*
	 *
	 */
	public List findByExample(Game instance) {
		log.debug("finding Game instance by example");
		try {
			List results = getSession().createCriteria(
					"com.newbingo.entity.Game").add(Example.create(instance))
					.list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/*
	 *
	 */
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Game instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Game as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/*
	 * 
	 */
	public List findByGamename(Object gamename) {
		return findByProperty(GAMENAME, gamename);
	}

	/*
	 *
	 */
	public List findByType(Object type) {
		return findByProperty(TYPE, type);
	}

	public List findByRule(Object rule) {
		return findByProperty(RULE, rule);
	}

	/*
	 *查询所有游戏
	 */
	public List findAll() {
		log.debug("finding all Game instances");
		try {
			String queryString = "from Game";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/*
	 *
	 */
	public Game merge(Game detachedInstance) {
		log.debug("merging Game instance");
		try {
			Game result = (Game) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/*
	 *
	 */
	public void attachDirty(Game instance) {
		log.debug("attaching dirty Game instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/*
	 *
	 */
	public void attachClean(Game instance) {
		log.debug("attaching clean Game instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/**
	 * ָ分页查询方法
	 * 
	 * @param start
	 * @param limit
	 * @return type 游戏类型
	 */
	public Pager findByPage(int start, int limit, Object type) {
		Pager pager = new Pager();
		Criteria cri = this.getSession().createCriteria(Game.class);
		// 判断是否有查询类型条件
		if (type != null) {
			cri.add(Expression.eq("type", type));
		}
		cri.setProjection(Projections.rowCount());
		pager.setTotalSize(((Number) cri.uniqueResult()).intValue());
		cri.setProjection(null);
		cri.setResultTransformer(Criteria.ROOT_ENTITY);
		cri.setFirstResult(start);
		cri.setMaxResults(limit);
		List gameList = cri.list();
		pager.setData(gameList);
		return pager;
	}




}
